//
//  HomeViewController.m
//  weibo
//
//  Created by qiuming on 15/12/21.
//  Copyright © 2015年 qiuming. All rights reserved.
//

#import "HomeViewController.h"
#import "Extensions.h"
#import "VisitorViewController.h"
#import "StatusModel.h"
#import "StatusListViewModel.h"
#import "StatusCell.h"

NSString *const statusCellReusedID = @"statusCellReusedID";

@interface HomeViewController ()
/// 微博数据数组
@property (strong, nonatomic) StatusListViewModel *statusListViewModel;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUIWithImageName:nil message:@"关注一些人，回这里看看有什么惊喜" loginSuccessBlock:^{
        
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonWithTitle:nil imageName:@"navigationbar_friendsearch" target:self action:@selector(friendSearch)];
        
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonWithTitle:nil imageName:@"navigationbar_pop" target:self action:@selector(scanLife)];
        
    }];
    [self prepareForTableView];
    
    // 加载微博数据
    [self loadStatus];
}

- (void)loadStatus {
    
    [self.statusListViewModel loadStatusWithCompleted:^(BOOL isSuccess) {
        if (!isSuccess) {
            DDLogError(@"加载出错");
            return ;
        }
        
        [self.tableView reloadData];
        
    }];
}

- (void)prepareForTableView {
    [self.tableView registerClass:[StatusCell class] forCellReuseIdentifier:statusCellReusedID];
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.statusListViewModel.statusList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:statusCellReusedID forIndexPath:indexPath];
    
    StatusViewModel *viewModel = self.statusListViewModel.statusList[indexPath.row];
    
    cell.ViewModel = viewModel;
    
    return cell;
}

/// 二维码
- (void)scanLife {
    DDLogError(@"二维码");
}

/// 搜索好友
- (void)friendSearch {
    NSLog(@"搜索好友");
}

#pragma mark - 懒加载 
- (StatusListViewModel *)statusListViewModel {
    if (_statusListViewModel == nil) {
        _statusListViewModel = [[StatusListViewModel alloc]init];
    }
    return _statusListViewModel;
}


@end
